import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Dashboard from '../views/Dashboard.vue'
import UserList from '../views/UserList.vue'
import ResetPassword from '../views/ResetPassword.vue'
import Register from '../views/Register.vue'
import ApiTest from '../views/ApiTest.vue'

const routes = [
    { path: '/', name: 'Login', component: Login },
    { path: '/login', name: 'LoginPage', component: Login },
    { path: '/home', name: 'Home', component: Dashboard, meta: { requiresAuth: true } },
    { path: '/user-list', name: 'UserList', component: UserList, meta: { requiresAuth: true } },
    { path: '/device-list', name: 'DeviceList', component: () => import('../views/DeviceList.vue'), meta: { requiresAuth: true } },
    { path: '/profile', name: 'Profile', component: () => import('../views/Profile.vue'), meta: { requiresAuth: true } },
    { path: '/reset-password', name: 'ResetPassword', component: ResetPassword },
    { path: '/register', name: 'Register', component: Register },
    { path: '/api-test', name: 'ApiTest', component: ApiTest }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth && !localStorage.getItem('token')) {
        next('/')
    } else {
        next()
    }
})

export default router